import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
// import { useStore } from '@/hooks'

const modules = import.meta.glob('../views/examples/*.vue')
// console.log('modules', modules)
// 定义一个路由数组
const routes: RouteRecordRaw[] = []
// 循环 modules 得到 routes
for (const key in modules) {
  const name = key.match(/^.*\/(.*).vue$/)?.[1]
  routes.push({
    path: '/' + name,
    name,
    component: modules[key]
  })
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    ...[
      {
        path: '/',
        name: 'home',
        redirect: '/films',
        component: () => import('@/views/index.vue'),
        children: [
          {
            path: '/films',
            name: 'films',
            component: () => import('@/views/films/index.vue')
          },
          {
            path: '/cinemas',
            name: 'cinemas',
            component: () => import('@/views/cinemas/index.vue')
          },
          {
            path: '/center',
            name: 'center',
            component: () => import('@/views/center/index.vue')
          }
        ]
      },
      {
        path: '/cinemas/search',
        name: 'cinemas-search',
        component: () => import('@/views/cinemas/search.vue')
      },
      {
        path: '/cinema/:cinemaId/film/:filmId?/:date?',
        name: 'schedule',
        component: () => import('@/views/schedule/index.vue')
      },
      {
        path: '/city',
        name: 'city',
        component: () => import('@/views/city/index.vue')
      }
      // {
      //   path: '/demo-list',
      //   name: 'demo-list',
      //   component: () => import('@/views/examples/demo-list.vue')
      // },
      // {
      //   path: '/demo-fancy-list',
      //   name: 'demo--fancy-list',
      //   component: () => import('@/views/examples/demo-fancy-list.vue')
      // },
      // {
      //   path: '/demo-fancy-list-two',
      //   name: 'demo--fancy-list-two',
      //   component: () => import('@/views/examples/demo-fancy-list-two.vue')
      // }
    ],
    ...routes
  ]
})

// router.beforeEach((to) => {
//   // 获取 cityStore
//   const { cityStore } = useStore()

//   if (to.name !== 'city' && !cityStore.curCityId) {
//     return { name: 'city' }
//   }
// })

export default router
